public void handleSessionEvent(GearmanSessionEvent event)
     throws IllegalArgumentException, IllegalStateException {
   GearmanPacket p = event.getPacket();
   GearmanJobServerSession s = event.getSession();
   GearmanPacketType t = p.getPacketType();
   LOG.debug(
       "Worker "
           + this
           + " handling session event"
           + " ( Session = "
           + s
           + " Event = "
           + t
           + " )");
   switch (t) {
     case JOB_ASSIGN:
       // TODO Figure out what the right behavior is if JobUUIDRequired was false when we submitted
       // but is now true
       taskMap.remove(s);
       addNewJob(event);
       break;
     case JOB_ASSIGN_UNIQ:
       // TODO Figure out what the right behavior is if JobUUIDRequired was true when we submitted
       // but is now false
       taskMap.remove(s);
       addNewJob(event);
       break;
     case NOOP:
       taskMap.remove(s);
       break;
     case NO_JOB:
       GearmanTask preSleepTask =
           new GearmanTask(
               new GrabJobEventHandler(s),
               new GearmanPacketImpl(
                   GearmanPacketMagic.REQ, GearmanPacketType.PRE_SLEEP, new byte[0]));
       taskMap.put(s, preSleepTask);
       s.submitTask(preSleepTask);
       break;
     case ECHO_RES:
       break;
     case OPTION_RES:
       break;
     case ERROR:
       s.closeSession();
       break;
     default:
       LOG.warn(
           "Received unknown packet type " + t + " from session " + s + ". Closing connection.");
       s.closeSession();
   }
 }
  public void handleSessionEvent(GearmanSessionEvent event)
      throws IllegalArgumentException, IllegalStateException {
    GearmanPacket p = event.getPacket();
    GearmanJobServerSession s = event.getSession();
    GearmanPacketType t = p.getPacketType();
    Map<JobHandle, GearmanJobImpl> jobsMaps = sessionJobsMap.get(s);

    switch (t) {
      case JOB_CREATED:
        if (jobAwatingCreation == null) {
          throw new IllegalStateException(
              "Recevied job creation " + "message but have not job awaiting submission.");
        }
        if (!jobAwatingCreation.isBackgroundJob()) {
          jobsMaps.put(new JobHandle(jobAwatingCreation), jobAwatingCreation);
        }
        break;
      case WORK_DATA:
      case WORK_STATUS:
      case WORK_WARNING:
      case WORK_COMPLETE:
      case WORK_FAIL:
      case WORK_EXCEPTION:
        JobHandle handle =
            new JobHandle(p.getDataComponentValue(GearmanPacket.DataComponentName.JOB_HANDLE));
        GearmanJobImpl job = jobsMaps.get(handle);
        if (job == null) {
          LOG.warn(
              "Client received packet from server"
                  + " for unknown job ( job_handle = "
                  + handle
                  + " packet = "
                  + t
                  + " )");
        } else {
          job.handleEvent(p);
          if (job.isDone()) {
            jobsMaps.remove(handle);
          }
        }
        break;
      case ERROR:
        String errCode =
            ByteUtils.fromUTF8Bytes(
                p.getDataComponentValue(GearmanPacket.DataComponentName.ERROR_CODE));
        String errMsg =
            ByteUtils.fromUTF8Bytes(
                p.getDataComponentValue(GearmanPacket.DataComponentName.ERROR_TEXT));
        LOG.warn(
            "Received error code "
                + errCode
                + "( "
                + errMsg
                + " )"
                + " from session "
                + s
                + ". Shutting session down");
        shutDownSession(s);
        if (sessionsMap.isEmpty()) {
          shutdown();
        }
        break;
      default:
        LOG.warn(
            "received un-expected packet from Job"
                + " Server Session: "
                + p
                + ". Shutting down session");
        shutDownSession(s);
        if (sessionsMap.isEmpty()) {
          shutdown();
        }
    }
  }